package com.example.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.backend.entity.DingDan;
import com.example.backend.entity.GouWuChe;
import com.example.backend.entity.ShenFen;
import com.example.backend.entity.vo.Show;
import com.example.backend.mapper.DingDanMapper;
import com.example.backend.mapper.ShenFenMapper;
import com.example.backend.service.IDingDanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DingDanService implements IDingDanService {
    @Autowired
    private DingDanMapper dingDanMapper;
    @Autowired
    private ShenFenMapper shenFenMapper;

    @Override
    public boolean addDingDan(DingDan dingDan) {
        return dingDanMapper.insert(dingDan) > 0;
    }

    @Override
    public boolean deleteDingDan(Integer ddid) {
        return dingDanMapper.deleteById(ddid) > 0;
    }

    @Override
    public boolean updateDingDan(DingDan dingDan) {
        return dingDanMapper.updateById(dingDan) > 0;
    }

    @Override
    public List<DingDan> queryDingDan() {
        return dingDanMapper.selectList(null);
    }

    @Override
    public DingDan queryDingDanById(Integer ddid) {
        return dingDanMapper.selectById(ddid);
    }

    @Override
    public List<DingDan> queryDingDanByYhId(Integer yhid) {
        return dingDanMapper.selectList(new QueryWrapper<DingDan>().eq("yhid", yhid));
    }

    @Override
    public Show queryDingDanByUserId(Integer yhid) {
        DingDan dingDan = dingDanMapper.selectById(yhid);
        ShenFen user = shenFenMapper.selectById(yhid);
        if(user == null){
            throw new RuntimeException("用户不存在，yhid=" + yhid);
        }
        Show show = new Show();
        show.setYhid(user.getYhid());
        show.setPhone(user.getPhone());
        show.setDdid(dingDan.getDdid());
        System.out.println(show);
        return show;
    }

    @Override
    public List<DingDan> queryDingDanBySellerId(Integer sjid) {
        return dingDanMapper.selectList(new LambdaQueryWrapper<DingDan>()
                .eq(DingDan::getSjid, sjid)
        );
    }

    @Override
    public DingDan queryDingDanByUserIdAndDate(Integer yhid, String ddsj) {
        return dingDanMapper.queryDingDanByYhidAndDdsj(yhid, ddsj);
    }

    @Override
    public List<DingDan> querydd(Integer sjid) {
        return dingDanMapper.querydd(sjid);
    }

    @Override
    public List<DingDan> queryAllDingDanBySjid(Integer sjid) {
        return dingDanMapper.queryAllDingDanBySjid(sjid);
    }

    @Override
    public List<DingDan> queryDaiPeiSongDingDanBySjid(Integer sjid) {
        return dingDanMapper.queryDaiPeiSongDingDanBySjid(sjid);
    }
}
